retour/
error.rs

1//! Error types and utilities.
2
3use std::error::Error as StdError;
4use std::fmt;
5
6/// The result of a detour operation.
7pub type Result<T> = ::std::result::Result<T, Error>;
8
9/// A representation of all possible errors.
10#[derive(Debug)]
11pub enum Error {
12  /// The address for the target and detour are identical
13  SameAddress,
14  /// The address does not contain valid instructions.
15  InvalidCode,
16  /// The address has no available area for patching.
17  NoPatchArea,
18  /// The address is not executable memory.
19  NotExecutable,
20  /// The detour is not initialized.
21  NotInitialized,
22  /// The detour is already initialized.
23  AlreadyInitialized,
24  /// The system is out of executable memory.
25  OutOfMemory,
26  /// The address contains an instruction that prevents detouring.
27  UnsupportedInstruction,
28  /// A memory operation failed.
29  RegionFailure(region::Error),
30}
31
32impl StdError for Error {
33  fn source(&self) -> Option<&(dyn StdError + 'static)> {
34    if let Error::RegionFailure(error) = self {
35      Some(error)
36    } else {
37      None
38    }
39  }
40}
41
42impl fmt::Display for Error {
43  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44    match self {
45      Error::SameAddress => write!(f, "Target and detour address is the same"),
46      Error::InvalidCode => write!(f, "Address contains invalid assembly"),
47      Error::NoPatchArea => write!(f, "Cannot find an inline patch area"),
48      Error::NotExecutable => write!(f, "Address is not executable"),
49      Error::NotInitialized => write!(f, "Detour is not initialized"),
50      Error::AlreadyInitialized => write!(f, "Detour is already initialized"),
51      Error::OutOfMemory => write!(f, "Cannot allocate memory"),
52      Error::UnsupportedInstruction => write!(f, "Address contains an unsupported instruction"),
53      Error::RegionFailure(ref error) => write!(f, "{}", error),
54    }
55  }
56}
57
58impl From<region::Error> for Error {
59  fn from(error: region::Error) -> Self {
60    Error::RegionFailure(error)
61  }
62}